﻿using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using dpa.Library.Services;

namespace dpa.Library.ViewModels;

public partial class MainViewModel(IMenuNavigationService menuNavigationService) : ViewModelBase
{
    [ObservableProperty] private ViewModelBase _content;

    public ObservableCollection<ViewModelBase> ContentStack { get; } = [];

    [ObservableProperty] private bool _isPaneOpen;

    [RelayCommand]
    private void OpenPane() => IsPaneOpen = true;

    [RelayCommand]
    private void ClosePane() => IsPaneOpen = false;

    [ObservableProperty] private string _title = "AirportInfo";

    public void PushContent(ViewModelBase content) => ContentStack.Insert(0, Content = content);

    public void SetMenuAndContent(string view, ViewModelBase content)
    {
        ContentStack.Clear();
        PushContent(content);
        SelectedMenuItem =
            MenuItem.MenuItems.FirstOrDefault(p => p.View == view);
        Title = SelectedMenuItem.Name;
        IsPaneOpen = false;
    }

    [ObservableProperty] private MenuItem _selectedMenuItem;

    [RelayCommand]
    private void GoBack()
    {
        if (ContentStack.Count <= 1)
        {
            return;
        }

        ContentStack.RemoveAt(0);
        Content = ContentStack[0];
    }

    [RelayCommand]
    private void OnMenuTapped()
    {
        if (SelectedMenuItem is null)
        {
            return;
        }

        menuNavigationService.NavigateTo(SelectedMenuItem.View);
    }
}

public class MenuItem
{
    public string View { get; private init; }
    public string Name { get; private init; }

    private MenuItem()
    {
    }

    private static MenuItem TodayView =>
        new() { Name = "今日推荐", View = MenuNavigationConstant.TodayView };

    private static MenuItem QueryView =>
        new() { Name = "诗词搜索", View = MenuNavigationConstant.QueryView };

    private static MenuItem FavoriteView =>
        new() { Name = "诗词收藏", View = MenuNavigationConstant.FavoriteView };
    
    private static MenuItem FileDropView =>
        new() { Name = "文件导入", View = MenuNavigationConstant.FileDropView };

    public static IEnumerable<MenuItem> MenuItems { get; } =
    [
        //TodayView, QueryView, FavoriteView, 
        FileDropView
    ];
}